/* eslint-disable */
// import axios from 'axios'
import store from '../store'
import qs from 'qs'
import {
  Dialog,
  Toast
} from 'mand-mobile'

axios.defaults.timeout = 600000
axios.defaults.baseURL = process.env.VUE_APP_API_ROOT
axios.defaults.headers.post['Content-Type'] = 'application/json charset=UTF-8'
// request拦截器
axios.interceptors.request.use(
  config => {
    // 乐享保token的接口lxbToken为true。同时有需要formdata的入参
    if(
      (config.method === 'post' && (config.data.lxbToken === true || typeof(config.data) === 'string'))
      || (config.method === 'get' && config.params.lxbToken === true)
    ){
      //接口中asker=esp 获取espToken 
      console.log('config.data',config)
      let flag = typeof(config.data) === 'string'
      if(config.data && config.data.lxbToken != true && flag && config.data.includes('asker=esp') == true){
        if (store.getters.getToken) {
          config.headers['token'] = store.getters.getToken
          if(store.getters.getSystemFlag == 'outer'){
            config['whoseToken'] = 'outerToken'
          }else {
            config['whoseToken'] = 'espToken'
          }
        }
        return config
      }
      console.log(',,,,,,,,,,,,,,,,,,,,,',store.getters.getLxbToken)
      if (store.getters.getLxbToken) {
        config.headers['token'] = store.getters.getLxbToken
        config['whoseToken'] = 'lxbToken'
      }
    }else{
      if (store.getters.getToken) {
        config.headers['token'] = store.getters.getToken
        if(store.getters.getSystemFlag == 'outer'){
          config['whoseToken'] = 'outerToken'
        }else {
          config['whoseToken'] = 'espToken'
        }
      }
    }
    return config
  },
  error => {
    console.log('。。。。。。。',error)
    return Promise.reject(error)
  }
)

// respone拦截器  //拦截响应response，并做一些错误处理
axios.interceptors.response.use(

  response => {
    console.log(response)
    // 通用逻辑，请求出错，全屏弹层提示
    if ('true' === process.env.VUE_APP_USE_MOCK && !response.data) {
      return response
    }

    // oss 上传文件put请求，根据方法及状态码判断
    if(response.config.method === 'put') {
      if(response.status === 200) {
        return true
      }else {
        return false
      }
    }

    const data = response.data

    // 判断是不是只读json(oss的静态数据)
    if(response.config.params && response.config.params.ossStatic == true){
      return data
    }

    if (data && data.code === 0) {
      return response.data.content
    } else {
      //处理错误
      if (data.code === 40002) { //此接口没有授权 由于您长时间未操作本次登陆失效，请点击重新登录
        alertReloadMessage('由于您长时间未操作本次登陆失效，请点击重新登录',response)
      } else if (data.code === 5001) { //此次请求ajax超时  本次操作请求超时，请重新操作
        alertMessage('本次操作请求超时，请重新操作')
      } else {
        alertMessage('系统请求异常，请重新打开')
      }

    }
  },
  error => {
    if (error.message.indexOf('timeout') != -1) {
      error.message = '本次操作请求超时，请重新操作'
    } else {
      error.message = '系统请求异常'
    }
    alertMessage(error.message)
    //return Promise.reject(error)
  }
)

function alertReloadMessage(message, { config }) {
  Dialog.closeAll()
  Toast.hide()
  Dialog.alert({
    content: message,
    confirmText: '重新登录',
    onConfirm: () => {
      window.location.reload()
    },
  })
}

function alertMessage(message) {
  Toast.failed(message, 3000, true)
}

export default {
  get(url, params = {}) {
    var data = params || {}
    data['sendTime'] = new Date().getTime()
    return axios.get(url, {
      params: data
    })
  },
  post(url, params = {data: {},contentType:''}) {
    if (params.contentType && params.contentType == 'form') {
      return axios.post(url, qs.stringify(params.data), {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    } else {
      return axios.post(url, params.data || params)
    }
  },
  getFile(url, params = {}) {
    var data = params || {}
    data['sendTime'] = new Date().getTime()
    return axios.get(url, {
      responseType: 'blob',
      params: data
    })
  },
  getCache (url, params = {}) {
    var data = params || {}
    return axios.get(url, {
      params: data
    })
  },
  put(url, params) {
    return axios.put(url, params, {
      headers: {
        'Content-Type': 'application/octet-stream'
      }
    })
  }
}